package com.example.myaccount.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myaccount.R;
import com.example.myaccount.activity.AddFlowActivity;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.bean.DailyStatistics;
import com.example.myaccount.bean.Flow;
import com.example.myaccount.constant.MyConst;
import com.example.myaccount.database.DatabaseHelper;
import com.example.myaccount.util.ResourceUtil;

import java.util.ArrayList;

public class RvDailyFlowAdapter extends RecyclerView.Adapter<RvDailyFlowAdapter.ViewHolder>{

    public ArrayList<Flow> dailyFlowList;


    public RvDailyFlowAdapter(ArrayList<Flow> dailyFlowList) {
        this.dailyFlowList = dailyFlowList;
    }

    public ArrayList<Flow> getDailyFlowList() {
        return dailyFlowList;
    }

    public void setDailyFlowList(ArrayList<Flow> dailyFlowList) {
        this.dailyFlowList = dailyFlowList;
    }

    @NonNull
    @Override
    public RvDailyFlowAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_each_flow, parent, false);
        // 实例化viewholder
        RvDailyFlowAdapter.ViewHolder viewHolder = new RvDailyFlowAdapter.ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RvDailyFlowAdapter.ViewHolder holder, int position) {
        Flow flow = dailyFlowList.get(position);
        holder.mIvIcon.setImageResource(ResourceUtil.getResId("ic_"+flow.getType()+"_checked",R.drawable.class));
        holder.mTvType.setText(MainActivity.getContext().getString(ResourceUtil.getResId("type_"+flow.getType(),R.string.class)));
        holder.mTvAnnotation.setText(flow.getAnnotation());
        holder.mTvAmount.setText(new Double(flow.getAmount()).toString());
    }

    @Override
    public int getItemCount() {
        return dailyFlowList == null ? 0 : dailyFlowList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{

        ImageView mIvIcon;
        TextView mTvType,mTvAnnotation,mTvAmount;

        public ViewHolder(View itemView){
            super(itemView);
            mIvIcon= (ImageView) itemView.findViewById(R.id.iv_icon);
            mTvType= (TextView) itemView.findViewById(R.id.tv_type);
            mTvAnnotation= (TextView) itemView.findViewById(R.id.tv_annotation);
            mTvAmount= (TextView) itemView.findViewById(R.id.tv_amount);

            itemView.setOnCreateContextMenuListener(this);
        }


        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuItem add = menu.add(Menu.NONE,1,1,MainActivity.getContext().getString(ResourceUtil.getResId("tips_update",R.string.class)));
            MenuItem delete = menu.add(Menu.NONE,2,2,MainActivity.getContext().getString(ResourceUtil.getResId("tips_delete",R.string.class)));

            add.setOnMenuItemClickListener(this);
            delete.setOnMenuItemClickListener(this);
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            DatabaseHelper databaseHelper = DatabaseHelper.getDefaultDatabaseHelper(MainActivity.getContext());
            int position = getAdapterPosition();
            switch (item.getItemId())
            {
                case 1:
                    SharedPreferences.Editor editor = MainActivity.getContext().getSharedPreferences(MyConst.SETTINGS,MODE_PRIVATE).edit();
                    editor.putString(MyConst.SETTINGS_STATE,MyConst.SETTINGS_STATE_UPDATE);
                    editor.putInt(MyConst.SETTINGS_FLOW_ID,getFlowId(position));
                    editor.commit();
                    Intent intent = new Intent(MainActivity.getContext(), AddFlowActivity.class);
                    MainActivity.getContext().startActivity(intent);
                    break;
                case 2:
                    databaseHelper.deleteFlow(getFlowId(position));
                    dailyFlowList.remove(position);
                    notifyDataSetChanged();
                    break;
            }
            return false;
        }
    }

    public int getFlowId(int position){
        return dailyFlowList.get(position).getId();
    }

}
